package nac.slick2dkryonet.client;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Client;
import java.io.IOException;
import nac.net.Network;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import nac.net.Network.Plane;

public class SlickClientMain extends BasicGame {

    private Client client;
    private Image planeImage;
    private Image landImage;
    private Plane[] planes = {null, null, null, null, null, null, null, null, null, null};
    private Plane myPlane;

    public SlickClientMain() {
        super("Lady");
    }

    @Override
    public void init(GameContainer gc)
            throws SlickException {
        planeImage = new Image("nac/slick2dkryonet/client/data/plane.png");
        landImage = new Image("nac/slick2dkryonet/client/data/land.jpg");
        client = new Client();
        client.start();
        Network.register(client);
 
   
        client.addListener(new ClientListener());
               
        new Thread("Connect") {

            @Override
            public void run() {
                try {
                    client.connect(5000, Network.HOST, Network.PORT);
                } catch (IOException ex) {
                    System.err.println(ex.getMessage());
                    System.exit(1);
                }

            }
        }.start();
        myPlane = new Plane();
        myPlane.id = client.getID();
        myPlane.rotation = 180;
        myPlane.x = 400;
        myPlane.y = 300;
        client.sendTCP(myPlane);
    }

    @Override
    public void update(GameContainer gc, int delta)
            throws SlickException {
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_A)) {
            myPlane.rotate(-0.2f, delta);
        }

        if (input.isKeyDown(Input.KEY_D)) {
            myPlane.rotate(0.2f, delta);
        }

        if (input.isKeyDown(Input.KEY_W)) {
            myPlane.move(0.4f, delta);
        }
        client.sendTCP(myPlane);
    }

    public void render(GameContainer gc, Graphics g)
            throws SlickException {

        landImage.draw(10, 10);
        if(planes == null){
            System.out.println("null render");
        }
        for (Plane p : planes) {
            if (p != null) {
                planeImage.setRotation(p.rotation);
                planeImage.draw(p.x, p.y);
            }

        }
    }

    public static void main(String[] args)
            throws SlickException {

        AppGameContainer app =
                new AppGameContainer(new SlickClientMain());
        app.setShowFPS(true);
        app.setDisplayMode(820, 620, false);
        app.setUpdateOnlyWhenVisible(false);
        app.setAlwaysRender(true);
        app.start();



    }

    private class ClientListener extends Listener {

        @Override
        public void received(Connection connection, Object object) {
            if (object == null) {
                System.out.println("null received");
            }
            planes = (Plane[]) object;

        }
    }
}
